-- 运营活动:首充返利


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 78, -- 活动id (78 首充返利)
    act_name = "首充返利", -- 活动名称
    act_desc = "首充返利而已啦", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1693278400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        precent = 30, --百分数 
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_78_check_cfg(cfg)
	if cfg == nil then return 20 end
	
	local config = cfg.config
	if config == nil then return 21 end
	

	return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_78_setup(cfg)	
	return 0
end

-- 卸载活动
function oper_act_78_unsetup(cfg)	
end

-- 活动开始
function oper_act_78_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_78_start = 1
    ssVar.oper_act_78_cmd = cfg.cmd_id
    ssVar.oper_act_78_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_78, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_78)
end

-- 活动结束
function oper_act_78_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_78_start = nil
    ssVar.oper_act_78_cmd = nil
    ssVar.oper_act_78_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end


    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_78].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local actorid = tonumber(Ranking.getId(item))
            local data = Ranking.getSub(item, 1)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data)
            end
          
            if dataTable.myRechargeData and dataTable.myRechargeData > 0 then
                for ii = 1, #config.Awards do
                    if dataTable.myRechargeData >= config.Awards[ii].rechargeLimitNum then
                        local awardNum = math.floor(config.Awards[ii].awardFactor * dataTable.myRechargeData)
                        local awardType = config.Awards[ii].awardType
                        local rewards = {{ type = awardType, count = awardNum, bind = 0},}

                        local title = string.format(Lang.ScriptTips.Operation202, cfg.act_name)
                        local content = string.format(Lang.ScriptTips.Operation203, cfg.act_name)
                        Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId078, Lang.LogTips.Operation078)
                        break
                    end
                end
            end
        end
    end
end

-- 打包数据
function oper_act_78_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_78_start then
        return
    end
    if not cfg then return end
    local config = cfg.config
    if not config then return end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_78, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myRechargeData then
        myData.myRechargeData = 0
    end

    local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (pack == nil) then
        return
    end
    DataPack.writeInt(pack, myData.myRechargeData)
    DataPack.flush(pack)

end

-- 操作，返回操作结果，0为成功
function oper_act_78_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_78_do cfg.config confg error")
        return
    end
end

--[[
-- 跨天
function oper_act_78_new_day_arrive(act_id) 
    
end
]]


function oper_act_78_withdrawyuanbao(sysarg, nYuanBao)
    if nYuanBao < 0 then
        return
    end
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_78_start then
        return
    end
    if not ssVar.oper_act_78_cmd then
        return
    end
    if not ssVar.oper_act_78_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_78_cmd, ssVar.oper_act_78_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then
        return
    end
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_78, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    if not myData.myRechargeData then
        myData.myRechargeData = 0
    end

    myData.myRechargeData = myData.myRechargeData + nYuanBao
    local time = System.getSystemTime()
    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end


    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_78, 1, newData)

    oper_act_78_packet_data(sysarg, cfg)
end